package com.project.web.controller;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.project.entity.ReaderInfo;

/**
 * @author jackiel@synnex.com
 * @version 1.0 by jackiel@synnex.com
 * @version 2.0 by travisl@synnex.com
 */
@Controller
public class LoginController extends GenericController {

	@RequestMapping(value = "/login")
	public String login() {
		return "login/login";
	}

	@RequestMapping(value = "/verify")
	public String verify(HttpSession session, Model model, String name,
			String password) {
		ReaderInfo readerInfo = readerInfoService.getReaderInfoByNameAndPsw(
				name, password);

		if (readerInfo == null) {
			model.addAttribute("error", "The username or password error!");
			return "login/login";
		} else {
			session.setAttribute("readerInfo", readerInfo);
			return "admin/index";
		}
	}

	@RequestMapping(value = "/logout")
	public String logout(HttpSession session) {
		session.removeAttribute("readerInfo");
		return "login/login";
	}
}
